package com.blog.utils;

import cn.hutool.json.JSONObject;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTPayload;
import cn.hutool.jwt.JWTUtil;
import com.blog.common.Constant;
import com.blog.system.Hr;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName JwtVerifyUtil
 * @Destription 其他微服务token解析
 * @Author 天堂小野花
 * @Date 2025/8/14  11:24
 * @Version 1.0
 */
public class JwtVerifyUtil {
    public Map<String, Object> parseJwt(String token,String key,Integer valitime){
        Map<String, Object> hr =new HashMap<String, Object>();
        if(token.startsWith("\"")){
            token = token.replaceAll("^\"|\"$", "");
        }
        JWT jwt = JWTUtil.parseToken(token);
        //验证是否正确
        boolean verify = jwt.setKey(key.getBytes()).verify();
        //验证是否过期
        boolean validate = jwt.validate(valitime);
        if(validate){
            //获取全部载荷信息
            JWTPayload payload = jwt.getPayload();
            //payload.getClaimsJson().forEach((k,v)-> System.out.println(k+"--->"+v));
            JSONObject claimsJson = payload.getClaimsJson();
            hr = (Map<String, Object>)claimsJson.get(Constant.TOKEN_USER_INFO);
        }
        return hr;
    }
}
